package com.se.dao.mysql;

import com.se.domain.mysql.UserDO;
import com.se.domain.vo.UserVO;
import com.se.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
@Slf4j
public class UserDao {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Value("${auth.user-token.redis-key-prefix}")
    private String userTokenRedisKeyPrefix;

    public UserVO getVOByUserId(Long userId){
        if(userId == null){
            return null;
        }
        UserDO userDO = userMapper.getByUserId(userId);
        if(userDO == null){
            return null;
        }
        return new UserVO(userDO);
    }
    public Map<Long, UserVO> mapVOByUserIds(List<Long> userIds){
        if(userIds.isEmpty()){
            return Collections.emptyMap();
        }
        List<UserDO> doList = userMapper.listByUserIds(userIds);

        Map<Long, UserVO> map = new HashMap<>();
        for (UserDO userDO : doList) {
            map.put(userDO.getUserId(), new UserVO(userDO));
        }
        return map;
    }
    public Map<Long, UserDO> mapByUserIds(List<Long> userIds){
        if(userIds.isEmpty()){
            return Collections.emptyMap();
        }
        List<UserDO> doList = userMapper.listByUserIds(userIds);
        Map<Long, UserDO> map = new HashMap<>();
        for (UserDO userDO : doList) {
            map.put(userDO.getUserId(), userDO);
        }
        return map;
    }

    public boolean existByUserId(Long userId) {
        return userMapper.existByUserId(userId);
    }

    public void storeUserIdUuidInRedis(Long userId, String uuid) {
        redisTemplate.opsForValue().set(this.redisUserIdKey(userId), uuid);
    }

    private String redisUserIdKey(Long userId){
        return this.userTokenRedisKeyPrefix +":"+userId;
    }

    public UserDO getByOpenId(String openId) {
        return userMapper.getByOpenId(openId);
    }

    public Long createByOpenId(String openid) {
        UserDO userDO = new UserDO();
        userDO.setOpenid(openid);
        userDO.setUname("默认用户名");
        userMapper.insertUser(userDO);
        Long userId = userDO.getUserId();
        if(userId == null){
            userId = userMapper.getUserIdByOpenId(openid);
        }
        return userId;
    }

    public void updateWxUserInfo(Long userId, String uname, String imgUrl) {
        userMapper.updateUserInfo(userId, uname, imgUrl);
    }

    public String getUUIDbyUserIdFromRedis(Long userId){
        if(userId == null){
            return null;
        }
        return redisTemplate.opsForValue().get(this.redisUserIdKey(userId));
    }

    public UserDO getByUserId(Long userId) {
        return userMapper.getByUserId(userId);
    }

    public List<String> listOpenIdBuUserIds(List<Long> userIds) {
        if(userIds.isEmpty()){
            return Collections.emptyList();
        }
        return userMapper.listOpenIdByUserIds(userIds);
    }
}
